package com.btl.component.crypto.aes.endpoint;

import com.btl.boot.core.dto.response.R;
import com.btl.component.crypto.aes.annotation.Decrypt;
import com.btl.component.crypto.aes.annotation.Encrypt;
import com.btl.component.crypto.aes.dto.RefreshKey;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 密钥 前端控制器
 * </p>
 *
 * @author Tim
 * @since 2025-06-19
 */
@Validated
@RestController
@Tag(name = "密钥")
@RequestMapping("/crypto")
public class CryptoController {

    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    /**
     * 密钥刷新
     *
     * @return R<T>
     */
    @PostMapping("refresh")
    @Decrypt
    @Encrypt
    R<Map<String, Object>> refresh(@RequestBody @Valid RefreshKey refreshKey) {
        redisTemplate.opsForValue()
                .set(refreshKey.getKeyId(), refreshKey.getKey(), 300, TimeUnit.SECONDS);
        return R.ok();
    }

}
